<?php

class Lawyer extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'/admin.php?c=auth&m=index');
        }
        $this->load->model('photomodel', 'photo');
        $this->load->model('lawyermodel', 'lawyer');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 5;
        $total = $this->lawyer->l_count();
        $offset = intval($this->input->get('per_page'));

        $config['base_url'] = base_url() . 'admin.php?c=lawyer&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '律师团队';
        $data['lawyers'] = $this->lawyer->l_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('lawyer/index', $data);
    }

    public function create() {
        $this->load->helper('form');
        $data['title'] = '添加律师成员';
        $data['photos'] = $this->getPhotos();
        $data['sex_array'] = $this->getSexArray();
        $this->load->view('lawyer/create', $data);
    }

    public function add() {
        $this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', '姓名', 'required');
		$this->form_validation->set_rules('content', '个人简介', 'required');
		
        if ($this->form_validation->run() == false) {
            $data['title'] = '添加律师成员';
            $data['photos'] = $this->getPhotos();
            $data['sex_array'] = $this->getSexArray();
            $this->load->view('lawyer/create', $data);
        } else {
            $lawyer['type'] = 1;
            $lawyer['photo_id'] = $this->input->post('photo');
            $lawyer['sex'] = $this->input->post('sex');
            $lawyer['name'] = $this->input->post('name');
            $lawyer['content'] = $this->input->post('content');
            $lawyer['created_at'] = date('Y-m-d H:i:s', time());

            if ($this->db->insert('lawyer', $lawyer)) {
                $this->session->set_flashdata('notice', '添加成功！');
            } else {
                $this->session->set_flashdata('error', '添加失败，数据库错误！');
            }
            redirect(base_url().'/admin.php?c=lawyer&m=index');
        }
    }

    public function edit() {
        $this->load->helper('form');
        $id = $this->getId();
        if ($lawyer = $this->lawyer->find($id)) {
            $data['title'] = '编辑' . $lawyer->name . '的资料';
            $data['photos'] = $this->getPhotos();
            $data['lawyer'] = $lawyer;
            $data['sex_array'] = $this->getSexArray();
            $this->load->view('lawyer/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        if ($lawyer = $this->lawyer->find($id)) {
            $this->load->library('form_validation');
			$this->form_validation->set_rules('name', '姓名', 'required');
			$this->form_validation->set_rules('content', '个人简介', 'required');
            if ($this->form_validation->run() == false) {
                $data['title'] = '编辑' . $lawyer->name . '的资料';
                $data['photos'] = $this->getPhotos();
                $data['lawyer'] = $lawyer;
                $data['sex_array'] = $this->getSexArray();
                $this->load->view('lawyer/edit', $data);
            } else {
                $l['photo_id'] = $this->input->post('photo');
                $l['sex'] = $this->input->post('sex');
                $l['name'] = $this->input->post('name');
                $l['content'] = $this->input->post('content');
                if ($this->db->update('lawyer', $l, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '修改成功！');
                } else {
                    $this->session->set_flashdata('error', '修改失败，数据库错误！');
                }
                redirect(base_url().'/admin.php?c=lawyer&m=index');
            }
        } else {
            show_404();
        }
    }

    public function delete() {
        $id = $this->getId();
        if ($this->lawyer->find($id)) {
            if ($this->db->delete('lawyer', array('id' => $id))) {
                $this->session->set_flashdata('notice', '删除成功！');
                redirect(base_url().'/admin.php?c=lawyer&m=index');
            }
        } else {
            show_404();
        }
    }

    private function getPhotos() {
        $photos[0] = '>> select <<';
        $query = $this->db->get('photo');
        foreach ($query->result() as $photo) {
            $photos[$photo->id] = $photo->label;
        }
        return $photos;
    }

    private function getId() {
        return intval($this->input->get('id'));
    }

    private function getSexArray() {
        return array(
            '0' => '女',
            '1' => '男'
        );
    }

}

?>
